package com.rain.shoppingscienceproj.ui.activity.register;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import com.rain.shoppingscienceproj.R;
import com.rain.shoppingscienceproj.http.BaseSubscriber;
import com.rain.shoppingscienceproj.http.ExceptionHandle;
import com.rain.shoppingscienceproj.http.RetrofitClient;
import com.rain.shoppingscienceproj.ui.activity.BaseActivity;
import com.rain.shoppingscienceproj.ui.utile.DeviceUtils;
import com.rain.shoppingscienceproj.ui.utile.TitleUtils;
import com.rain.shoppingscienceproj.utils.AppManager;
import com.rain.shoppingscienceproj.utils.LoadingDialog;
import com.rain.shoppingscienceproj.utils.TimeUtils;
import com.rain.shoppingscienceproj.utils.ToolPhone;
import com.rain.shoppingscienceproj.utils.toast.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;

public class RegisterActivity extends BaseActivity {

    @BindView(R.id.et_mobile)
    EditText et_mobile;
    @BindView(R.id.et_code)
    EditText et_code;
    @BindView(R.id.tv_code)
    TextView tv_code;
    @BindView(R.id.et_password)
    EditText et_password;
    @BindView(R.id.et_notarize_password)
    EditText et_notarize_password;
    @BindView(R.id.btn_register)
    Button btn_register;
    int type;

    @OnClick(R.id.rl_back)
    void back() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        AppManager.getAppManager().addActivity(this);
        ButterKnife.bind(this);
        loadingDialog = new LoadingDialog(this, "正在加载...");
        type = getIntent().getIntExtra("type", 0);
        if (type == 1) {
            TitleUtils.TitleView(this, "注册");
            btn_register.setText("注册");
        } else {
            TitleUtils.TitleView(this, "忘记密码");
            btn_register.setText("提交");
        }

        et_mobile.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

                if (editable.toString().length() > 0) {
                    tv_code.setBackgroundResource(R.color.colorPrimary);
                    tv_code.setTextColor(getResources().getColor(R.color.white));
                    tv_code.setEnabled(true);
                } else {
                    tv_code.setTextColor(getResources().getColor(R.color.white));
                    tv_code.setBackgroundResource(R.color.code_color);
                    tv_code.setEnabled(false);
                }
            }
        });
    }


    @OnClick(R.id.tv_code)
    void code() {
        String mobile = et_mobile.getText().toString().trim();
        boolean isPhone = ToolPhone.isMobileNO(mobile);
        if (!isPhone) {
            ToastUtils.showShortToast("手机号码格式错误");
            return;
        }
        TimeUtils.countDown(60000, tv_code, RegisterActivity.this);
//        ToastUtils.showShortToast("发送成功");
        loadingDialog.show();
        RetrofitClient.getInstance(this).createBaseApi().code(mobile, new BaseSubscriber<ResponseBody>(this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                ToastUtils.showLongToast("网络错误");
                loadingDialog.dismiss();
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                super.onNext(responseBody);
                loadingDialog.dismiss();
                try {
                    String request = responseBody.string();
                    JSONObject json = new JSONObject(request);
                    int code = json.getInt("code");
                    if (code == 0) {
                        ToastUtils.showShortToast("发送成功");
                        TimeUtils.countDown(60000, tv_code, RegisterActivity.this);
                    } else {
                        ToastUtils.showShortToast(json.getString("message"));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }


    @OnClick(R.id.btn_register)
    void register() {

        String mobile = et_mobile.getText().toString().trim();
        if (mobile.isEmpty()) {
            ToastUtils.showLongToast("请输入手机号码");
            return;
        }
        boolean isPhone = ToolPhone.isMobileNO(mobile);
        if (!isPhone) {
            ToastUtils.showShortToast("手机号码格式错误");
            return;
        }
        String code = et_code.getText().toString().trim();
        if (code.isEmpty()) {
            ToastUtils.showLongToast("请输入验证码");
            return;
        }
        String password = et_password.getText().toString().trim();
        String passwordSure = et_notarize_password.getText().toString().trim();
        if (password.isEmpty()) {
            ToastUtils.showLongToast("请输入密码");
            return;
        }
        if (!password.equals(passwordSure)) {
            ToastUtils.showLongToast("两次密码不一致");
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("code", code);
        if (type == 1) {
            String deviceId = DeviceUtils.getUniqueId(this);
            map.put("userPhone", mobile);
            map.put("passWord", password);
            map.put("deviceid", deviceId);
            map.put("type", 0 + "");
            register(map);
        } else {
            map.put("phone", mobile);
            map.put("newPass", password);
            forget(map);
        }

    }

    private void forget(Map<String, String> map) {
        loadingDialog.dismiss();
        RetrofitClient.
                getInstance(this)
                .createBaseApi()
                .forgetpwd(map, new BaseSubscriber<ResponseBody>(this) {
                    @Override
                    public void onError(ExceptionHandle.ResponeThrowable e) {
                        ToastUtils.showLongToast("网络错误");
                        loadingDialog.dismiss();
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        super.onNext(responseBody);
                        loadingDialog.dismiss();
                        try {
                            String request = responseBody.string();
                            JSONObject json = new JSONObject(request);
                            int code = json.getInt("code");
                            if (code == 0) {
                                ToastUtils.showShortToast("提交成功");
                                finish();
                            } else {
                                ToastUtils.showShortToast(json.getString("message"));
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });


    }

    public void register(Map<String, String> map) {
        loadingDialog.show();
        RetrofitClient.getInstance(this).createBaseApi().register(map, new BaseSubscriber<ResponseBody>(this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                loadingDialog.dismiss();
                ToastUtils.showLongToast("网络错误");
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                super.onNext(responseBody);
                loadingDialog.dismiss();
                try {
                    String request = responseBody.string();
                    JSONObject json = new JSONObject(request);
                    int code = json.getInt("code");
                    if (code == 0) {
                        ToastUtils.showShortToast("注册成功");
                        finish();
                    } else {
                        ToastUtils.showShortToast(json.getString("message"));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
